package com.sanery.servlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sanery.dao.ListDaoImpl;
import com.sanery.entity.Wrong;
import com.sanery.util.UploadHelper;

@WebServlet("/edit")
public class EditServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Integer wrong_id = Integer.parseInt(request.getParameter("wrong_id"));
		ServletContext application = request.getServletContext();
		Wrong wrong_detail = ((Map<Integer, Wrong>) application.getAttribute("allList")).get(wrong_id);
		request.setAttribute("wrong_detail", wrong_detail);
		request.getRequestDispatcher("/WEB-INF/pages/edit.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ListDaoImpl dao = new ListDaoImpl();
		List list = new ArrayList();
		try {
			String prjdir = request.getServletContext().getRealPath("/"); // D:\APP\Java\apache-tomcat-8.5.11\webapps\ErrorRegister\
			String dir = new File(prjdir).getParent() + "\\imagesdir"; // D:\APP\Java\apache-tomcat-8.5.11\webapps\imagesdir

			Map<String, String> map = UploadHelper.upload2(request, dir, 10485760, ".jpg.png.gif.bmp");
			list.add(map.get("title"));
			list.add(map.get("answer"));
			list.add(map.get("errimg"));
			list.add(map.get("createtime"));
			list.add(map.get("dealtype"));
			list.add(map.get("dealtime"));
			list.add(map.get("tip"));
			list.add(map.get("wrong_id"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		dao.updateWrong(list);
		response.sendRedirect("list");
	}

}
